public class MinPathSum {
    public static void main(String[] args) {
        // 输入字符串转二维数组
        // [[1,3,1],[1,5,1],[4,2,1]]
        String input = args[0].substring(1, args[0].length()-1);
        // 通过[1,3,1]取列数
        String firstRow = input.substring(1,input.indexOf("]"));
        System.out.println("firstRow:"+firstRow);
        int columns = firstRow.split(",").length;
        System.out.println("列数："+columns);
        String items = input.replaceAll("\\[", "").replaceAll("]", "");
        System.out.println("items:"+items);
        String[] itemsArr = items.split(",");
        int rows = itemsArr.length / columns;
        int[][] arr = new int[rows][columns];
        int index = 0;
        for(int i = 0; i < rows; i ++) {
            for(int j = 0; j < columns; j++) {
                arr[i][j] = Integer.parseInt(itemsArr[index]);
                index++;
            }
        }

        MinPathSum minPathSum = new MinPathSum();
        int result = minPathSum.minPathSum(arr);
        System.out.println("结果："+result);
    }
    public int minPathSum(int[][] grid) {
        int m = grid.length; // 第一维的长度
        int n = grid[0].length; // 第二维的长度
        int[][] dp = new int[m][n];
        dp[0][0] = grid[0][0];
        for(int i = 1; i < m; i++) {
            dp[i][0] = dp[i-1][0]+grid[i][0];
        }
        for(int j = 1; j < n; j++) {
            dp[0][j] = dp[0][j-1]+grid[0][j];
        }
        for(int i = 1; i < m; i++) {
            for(int j = 1; j < n; j++) {
                dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i][j];
            }
        }
        return dp[m-1][n-1];
    }
}
